<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="1200"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{ top: '93px', left: '154px' }"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="取消"
      okText="保存"
      style="top: 10%; height: 80%"
    >
      <template slot="footer">
        <a-button key="back" v-if="isReadOnly" @click="handleCancel"> 取消 </a-button>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="prescriptModal">
          <a-row class="form-row" :gutter="24">
            <a-col :span="24 / 2">
              <a-form-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="客户"
                data-step="1"
                data-title="客户"
                data-intro="客户必须选择，如果发现需要选择的客户尚未录入，可以在下拉框中点击新增客户进行录入。
                          特别注意，客户如果录入之后在下拉框中不显示，请检查是否给当前用户分配对应的客户权限"
              >
                <a-select
                  placeholder="请选择客户"
                  v-decorator="['customerId', validatorRules.customerId]"
                  :dropdownMatchSelectWidth="false"
                  showSearch
                  optionFilterProp="children"
                >
                  <div slot="dropdownRender" slot-scope="menu">
                    <v-nodes :vnodes="menu" />
                    <a-divider style="margin: 4px 0" />
                    <div
                      v-if="quickBtn.customer"
                      style="padding: 4px 8px; cursor: pointer"
                      @mousedown="(e) => e.preventDefault()"
                      @click="addCustomer"
                    >
                      <a-icon type="plus" /> 新增客户
                    </div>
                  </div>
                  <a-select-option v-for="(item, index) in cusList" :key="index" :value="item.id">
                    {{ item.supplier }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="验光师">
                <a-input placeholder="请输入验光师" v-decorator="['optometrist', validatorRules.optometrist]" />
              </a-form-item>
            </a-col>
          </a-row>
          <a-row class="form-row" :gutter="24">
            <a-table
              ref="table"
              bordered
              size="small"
              rowKey="title"
              :dataSource="prescriptDataSource"
              :loading="confirmLoading"
              :pagination="false"
            >
              <a-table-column
                key="title"
                title="双光/渐进"
                data-index="title"
                align="center"
              />
              <a-table-column key="sph" title="球镜" :width="90" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number
                    v-if="index == 0"
                    v-model="model.rSph"
                    style="width: 100%"
                    :precision="2"
                    :step="0.25"
                  />
                  <a-input-number
                    v-if="index == 1"
                    v-model="model.lSph"
                    style="width: 100%"
                    :precision="2"
                    :step="0.25"
                  />
                </template>
              </a-table-column>
              <a-table-column key="cyl" title="柱镜" :width="90" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number
                    v-if="index == 0"
                    v-model="model.rCyl"
                    style="width: 100%"
                    :precision="2"
                    :step="0.25"
                  />
                  <a-input-number
                    v-if="index == 1"
                    v-model="model.lCyl"
                    style="width: 100%"
                    :precision="2"
                    :step="0.25"
                  />
                </template>
              </a-table-column>
              <a-table-column key="ax" title="轴向" :width="90" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number v-if="index == 0" v-model="model.rAx" style="width: 100%" :precision="0" />
                  <a-input-number v-if="index == 1" v-model="model.lAx" style="width: 100%" :precision="0" />
                </template>
              </a-table-column>
              <a-table-column key="add" title="Add" :width="90" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number
                    v-if="index == 0"
                    v-model="model.rAdd"
                    style="width: 100%"
                    :precision="2"
                    :step="0.25"
                  />
                  <a-input-number
                    v-if="index == 1"
                    v-model="model.lAdd"
                    style="width: 100%"
                    :precision="2"
                    :step="0.25"
                  />
                </template>
              </a-table-column>
              <a-table-column key="bo" title="棱镜" :width="90" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number
                    v-if="index == 0"
                    v-model="model.rBo"
                    style="width: 100%"
                    :precision="2"
                    :step="0.25"
                  />
                  <a-input-number
                    v-if="index == 1"
                    v-model="model.lBo"
                    style="width: 100%"
                    :precision="2"
                    :step="0.25"
                  />
                </template>
              </a-table-column>

              <a-table-column key="farPd" title="远用瞳距(mm)" :width="120" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number v-if="index == 0" v-model="model.rFarPd" style="width: 100%" :precision="2" />
                  <a-input-number v-if="index == 1" v-model="model.lFarPd" style="width: 100%" :precision="2" />
                </template>
              </a-table-column>
              <a-table-column key="nearPd" title="近用瞳距(mm)" :width="120" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number
                    v-if="index == 0"
                    v-model="model.rNearPd"
                    style="width: 100%"
                    :precision="2"
                  />
                  <a-input-number
                    v-if="index == 1"
                    v-model="model.lNearPd"
                    style="width: 100%"
                    :precision="2"
                  />
                </template>
              </a-table-column>
              <a-table-column key="ph" title="瞳高(mm)" :width="90" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number v-if="index == 0" v-model="model.rPh" style="width: 100%" :precision="2" />
                  <a-input-number v-if="index == 1" v-model="model.lPh" style="width: 100%" :precision="2" />
                </template>
              </a-table-column>

              <a-table-column key="farBcva" title="远用VA" :width="120" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number
                    v-if="index == 0"
                    v-model="model.rFarBcva"
                    style="width: 100%"
                    :precision="1"
                  />
                  <a-input-number
                    v-if="index == 1"
                    v-model="model.lFarBcva"
                    style="width: 100%"
                    :precision="1"
                  />
                </template>
              </a-table-column>
              <a-table-column key="nearBcva" title="近用VA" :width="120" align="center">
                <template slot-scope="text, record, index">
                  <a-input-number
                    v-if="index == 0"
                    v-model="model.rNearBcva"
                    style="width: 100%"
                    :precision="1"
                  />
                  <a-input-number
                    v-if="index == 1"
                    v-model="model.lNearBcva"
                    style="width: 100%"
                    :precision="1"
                  />
                </template>
              </a-table-column>
            </a-table>
          </a-row>
        </a-form>
      </a-spin>

      <customer-modal ref="customerModalForm" @ok="customerModalFormOk"></customer-modal>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import { addPrescript, editPrescript, findBySelectCus } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
import { mixinDevice } from '@/utils/mixin'
import CustomerModal from '../../system/modules/CustomerModal'
import Vue from 'vue'
export default {
  name: 'PrescriptModal',
  mixins: [mixinDevice],
  components: {
    CustomerModal,
    VNodes: {
      functional: true,
      render: (h, ctx) => ctx.props.vnodes,
    },
  },
  data() {
    return {
      prescriptDataSource: [
        {
          title: 'OD',
        },
        {
          title: 'OS',
        },
      ],
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        customerId: {
          rules: [{ required: true, message: '请选择客户!' }],
        },
        optometrist: {
          rules: [{ required: true, message: '请输入验光师!' }],
        },
      },
      cusList: [],
      quickBtn: {
        vendor: false,
        customer: false,
        member: false,
        account: false,
        depot: false,
      },
    }
  },
  created() {},
  mounted() {
    this.initCustomer(0)
    this.initQuickBtn()
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'customerId',
            'rSph',
            'rCyl',
            'rAx',
            'rCk',
            'rBo',
            'rAdd',
            'rFarBcva',
            'rNearBcva',
            'rFarPd',
            'rNearPd',
            'rPh',
            'lSph',
            'lCyl',
            'lAx',
            'lCk',
            'lBo',
            'lAdd',
            'lFarBcva',
            'lNearBcva',
            'lFarPd',
            'lNearPd',
            'lPh',
            'optometrist'
          )
        )
        autoJumpNextInput('prescriptModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          console.log(formData, 'formData')

          let obj
          if (!this.model.id) {
            obj = addPrescript(formData)
          } else {
            obj = editPrescript(formData)
          }
          obj
            .then((res) => {
              if (res.code === 200) {
                that.$emit('ok')
              } else {
                that.$message.warning(res.data.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    initCustomer(isChecked) {
      let that = this
      findBySelectCus({}).then((res) => {
        if (res) {
          that.cusList = res
          if (isChecked && res.length > 0) {
            that.form.setFieldsValue({ customerId: res[0].id })
          }
        }
      })
    },
    initQuickBtn() {
      let btnStrList = Vue.ls.get('winBtnStrList') //按钮功能列表 JSON字符串
      if (btnStrList) {
        for (let i = 0; i < btnStrList.length; i++) {
          if (btnStrList[i].btnStr) {
            this.quickBtn.vendor =
              btnStrList[i].url === '/system/vendor' ? btnStrList[i].btnStr.indexOf(1) > -1 : this.quickBtn.vendor
            this.quickBtn.customer =
              btnStrList[i].url === '/system/customer' ? btnStrList[i].btnStr.indexOf(1) > -1 : this.quickBtn.customer
            this.quickBtn.member =
              btnStrList[i].url === '/system/member' ? btnStrList[i].btnStr.indexOf(1) > -1 : this.quickBtn.member
            this.quickBtn.account =
              btnStrList[i].url === '/system/account' ? btnStrList[i].btnStr.indexOf(1) > -1 : this.quickBtn.account
            this.quickBtn.depot =
              btnStrList[i].url === '/system/depot' ? btnStrList[i].btnStr.indexOf(1) > -1 : this.quickBtn.depot
          }
        }
      }
    },
    addCustomer() {
      this.$refs.customerModalForm.add()
      this.$refs.customerModalForm.title =
        '新增客户（提醒：如果找不到新添加的客户，请到用户管理检查是否分配了该客户权限）'
      this.$refs.customerModalForm.disableSubmit = false
    },
    customerModalFormOk() {
      this.initCustomer(1)
    },
  },
}
</script>
<style scoped></style>
